package com.bailan.foreat.utils;

import com.bailan.foreat.entity.Seller;
import com.bailan.foreat.service.SellerService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

public class SellerUtils {
    /**
     * 根据request获取Seller
     * @param request
     * @return
     */
    public static Seller getCurrentSeller(HttpServletRequest request){
        // Spring中获取ServletContext对象
        ServletContext sc = request.getServletContext();
        //获取ApplicationContext对象
        ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        //获取sellerService对象
        SellerService sellerService = ac.getBean(SellerService.class);
        //解析jwt
        String sellerTokenFromCookie = CookieUtils.getUserTokenFromCookie(request);
        String currentSellerAccount = JwtUtils.getAccountWithoutException(sellerTokenFromCookie);
        //获取user对象
        Seller currentSeller = sellerService.getSellerBySellerName(currentSellerAccount);
        return currentSeller;
    }
}
